package com.ruoyi.equipment.util;


import com.ruoyi.equipment.enums.QosEnum;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class EmqxUtils {
    private static InetAddress address = null;

    public static String getUrl() {
        try {
            if (address == null) {
                address = InetAddress.getLocalHost();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "http://" + address.getHostAddress() + ":8081";
    }

    //进行一个消息的订阅
    public static String autoSub(String clientId, String topics, QosEnum qos, String url) {
        Map<Object, Object> map = new HashMap<>();
        map.put("clientid", clientId);
        map.put("qos", 2);
        map.put("topic", topics);
        //进行一个消息的订阅
        return RestTemplateUtils.Post(map, url);
    }

    //自动取消消息的订阅
    public static String autoUnSub(String clientid, String topic, String url) {
        Map<String, Object> map = new HashMap<>();
        map.put("clientid", clientid);
        map.put("topic", topic);
        return RestTemplateUtils.Post(map, url);
    }

    //发布消息 指定编码
    public static String publish(String topic, String clientid, String payload, String encoding, QosEnum qos, Boolean retain, String url) {
        Map<String, Object> map = new HashMap<>();
        map.put("topic", topic);
        map.put("clientid", clientid);
        map.put("payload", payload);
        map.put("encoding", encoding);
        map.put("qos", 2);
        map.put("retain", retain);
        return RestTemplateUtils.Post(map, url);
    }

    //发布消息 未指定编码
    public static String publish(String topic, String clientid, String payload, QosEnum qos, Boolean retain, String url) {
        Map<String, Object> map = new HashMap<>();
        map.put("topic", topic);
        map.put("clientid", clientid);
        map.put("payload", payload);
        map.put("qos", 2);
        map.put("retain", retain);
        return RestTemplateUtils.Post(map, url);
    }

    //获得该服务器下所有的设备
    public static String getClient(String url) {
        return RestTemplateUtils.get(url);
    }

    //删除服务器下的某一设备
    public static void deleteClient(String url) {
        RestTemplateUtils.delete(url);
    }


}
